package uk.org.landeg.euler.problems;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;

public class Problem017 extends AbstractProblem{
	static final String digits[] = {
		"zero" , "one","two","three","four","five","six","seven","eight","nine"
	};
	
	static final String ten_mults[] = {
		"ten","hundred","thousand"
	};
	
	static final String tens[] = {
		"","ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"
	};
	
	static final String eleven_to_nineteen[] = {
		"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"
	};
	
	
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem017());
		launcher.solve();
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 17;
	}

	@Override
	public Object solve() {
		int chars = 0;
		int len;
		String words;
		for (int n = 1 ; n <= 1000 ; n++) {
			words = toWords(n).replace(" ", "");
			len = words.length();
			System.out.println(words + "(" + len + ")");
			chars += words.length();
		}
		return chars;
	}
	
	private String toWords (int n) {
		int d;
		boolean andAdded = true;
		String words = new String();
		// n > 999
		d = n/1000;
		if (d > 0) {
			n -= d * 1000;
			words += digits[d] + " thousand";
			andAdded = false;
		}
		
		// n > 99
		d = n/100;
		if (d > 0) {
			n -= d * 100;
			words += digits[d] + " hundred";
			andAdded = false;
		}
		
		
		// n > 10 && n < 20
		if (n > 10 && n < 20) {
			if (words.length() > 0) {
				words += " and";
			}
			andAdded = true;
			words += " " + eleven_to_nineteen[n-10];
			n  = 0;
		}
		// n > 10
		d = n/10;
		if (d > 0) {
			if (words.length() > 0) {
				if (!andAdded) {
					words += " and";
					andAdded = true;
				}
			}
			n -= d * 10;
			words += " " + tens[d];
		}
		if (n > 0) {
			if (words.length()>0 && !andAdded) {
				words += " and";
			}
			words += digits[n];
		}
		return words;
	}
}
